package com.fr.data.util.function;

import com.fr.base.FRContext;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.list.IntList;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.report.script.Calculator;
import com.fr.report.script.GroupResultSequenceNameSpace;
import com.fr.report.script.SingleRowNameSpace;
import com.fr.report.script.core.FArray;
import com.fr.report.script.core.parser.ConditionalOrExpression;
import com.fr.report.script.core.parser.DatasetFunctionCall;
import com.fr.report.script.core.parser.InterpreterError;
import com.fr.report.script.core.parser.UtilEvalError;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

public abstract class DataSetFunction
{
  public Object resolveDatasetFunction(DatasetFunctionCall paramDatasetFunctionCall)
  {
    String str = paramDatasetFunctionCall.getFnName().toLowerCase();
    Object localObject = null;
    if ("select".equals(str))
    {
      localObject = resolveFunction(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments(), 'S');
    }
    else if ("selectfirst".equals(str))
    {
      localObject = resolveFunction(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments(), 'S');
    }
    else if ("group".equals(str))
    {
      localObject = resolveFunction(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments(), 'G');
    }
    else if ("xselect".equals(str))
    {
      localObject = resolveXFunction(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments(), 'S');
    }
    else if ("xselectfirst".equals(str))
    {
      localObject = resolveFunction(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments(), 'S');
    }
    else if ("xgroup".equals(str))
    {
      localObject = resolveXFunction(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments(), 'G');
    }
    else
    {
      if ("colcount".equals(str))
        return resolveColumnCount(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments());
      if ("colname".equals(str))
        return resolveColumnName(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments());
      if ("column".equals(str))
      {
        localObject = resolveColumn(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments());
      }
      else
      {
        if ("value".equals(str))
          return resolveValue(paramDatasetFunctionCall.getSourceName(), paramDatasetFunctionCall.getArguments());
        FRContext.getLogger().log(Level.WARNING, "not supported function name: " + str);
        return null;
      }
    }
    if (localObject instanceof FArray)
    {
      FArray localFArray = (FArray)localObject;
      if (str.endsWith("selectfirst"))
        return ((localFArray.length() > 0) ? localFArray.elementAt(0) : null);
      return ((localFArray.length() == 1) ? localFArray.elementAt(0) : localFArray);
    }
    return localObject;
  }

  protected int[] fn_source_filter(ConditionalOrExpression paramConditionalOrExpression, int[] paramArrayOfInt, SingleRowNameSpace paramSingleRowNameSpace, Calculator paramCalculator)
  {
    if (paramConditionalOrExpression != null)
    {
      IntList localIntList = new IntList();
      for (int i = 0; i < paramArrayOfInt.length; ++i)
      {
        int j = paramArrayOfInt[i];
        paramSingleRowNameSpace.setRowIndex(j);
        try
        {
          if (Boolean.TRUE.equals(paramConditionalOrExpression.eval(paramCalculator)))
            localIntList.add(j);
        }
        catch (UtilEvalError localUtilEvalError)
        {
          String str = "error happens at calculate formula\nformula expression is " + paramConditionalOrExpression + "\nerror message is " + localUtilEvalError.getMessage();
          FRContext.getLogger().log(Level.WARNING, str, localUtilEvalError);
        }
      }
      paramArrayOfInt = localIntList.toArray();
    }
    return paramArrayOfInt;
  }

  protected Object[] fn_cc_ex_result(ConditionalOrExpression paramConditionalOrExpression, int[] paramArrayOfInt, TableData paramTableData, SingleRowNameSpace paramSingleRowNameSpace, Calculator paramCalculator)
  {
    int j;
    Object[] arrayOfObject = new Object[paramArrayOfInt.length];
    String str1 = paramConditionalOrExpression.toString();
    int i = -2147483648;
    i = col_name_2_col_index(paramTableData, str1);
    if (i >= -1)
    {
      for (j = 0; j < paramArrayOfInt.length; ++j)
        arrayOfObject[j] = DataUtils.getTableDataValue(paramTableData, paramArrayOfInt[j], i);
      return arrayOfObject;
    }
    try
    {
      str1 = paramConditionalOrExpression.eval(paramCalculator).toString();
      i = col_name_2_col_index(paramTableData, str1);
      if (i >= -1)
      {
        for (j = 0; j < paramArrayOfInt.length; ++j)
          arrayOfObject[j] = DataUtils.getTableDataValue(paramTableData, paramArrayOfInt[j], i);
        return arrayOfObject;
      }
    }
    catch (UtilEvalError localUtilEvalError1)
    {
    }
    catch (InterpreterError localInterpreterError1)
    {
    }
    GroupResultSequenceNameSpace localGroupResultSequenceNameSpace = new GroupResultSequenceNameSpace();
    paramCalculator.pushNameSpace(localGroupResultSequenceNameSpace);
    for (int k = 0; k < paramArrayOfInt.length; ++k)
    {
      String str2;
      int l = paramArrayOfInt[k];
      paramSingleRowNameSpace.setRowIndex(l);
      try
      {
        arrayOfObject[k] = paramConditionalOrExpression.eval(paramCalculator);
      }
      catch (UtilEvalError localUtilEvalError2)
      {
        str2 = "error happens at calculate formula\nformula expression: \"" + paramConditionalOrExpression + "\"" + "\nerror message: " + localUtilEvalError2.getMessage();
        FRContext.getLogger().log(Level.WARNING, str2, localUtilEvalError2);
      }
      catch (InterpreterError localInterpreterError2)
      {
        str2 = "\"" + paramConditionalOrExpression + "\"" + " can not be resolved as a column" + "\nformula expression: " + "\"" + paramConditionalOrExpression + "\"" + "\nerror message: " + localInterpreterError2.getMessage();
        FRContext.getLogger().log(Level.INFO, str2, localInterpreterError2);
        String str3 = paramConditionalOrExpression.toString();
        Arrays.fill(arrayOfObject, str3);
        break label346:
      }
    }
    label346: paramCalculator.removeNameSpace(localGroupResultSequenceNameSpace);
    return arrayOfObject;
  }

  protected int col_name_2_col_index(TableData paramTableData, String paramString)
  {
    int i = -2147483648;
    if (paramString.matches("^\\w+$"))
    {
      try
      {
        int j = 0;
        int l = paramTableData.getColumnCount();
        while (j < l)
        {
          if (ComparatorUtils.tableDataColumnNameEquals(paramString, paramTableData.getColumnName(j)))
          {
            i = j;
            break;
          }
          ++j;
        }
      }
      catch (TableDataException localTableDataException1)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException1.getMessage(), localTableDataException1);
      }
    }
    else if (paramString.matches("^#\\d+$"))
    {
      int k = Integer.parseInt(paramString.substring(1));
      try
      {
        if ((k <= paramTableData.getColumnCount()) && (k >= 0))
          i = k - 1;
      }
      catch (TableDataException localTableDataException2)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException2.getMessage(), localTableDataException2);
      }
    }
    return i;
  }

  public abstract Object resolveFunction(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression, char paramChar);

  public abstract Object resolveXFunction(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression, char paramChar);

  public abstract Object resolveColumnCount(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression);

  public abstract Object resolveColumnName(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression);

  public abstract Object resolveColumn(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression);

  public abstract Object resolveValue(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression);
}